/**
* userManageChart functions
* copyright@Catosoft.com reserved, 2016
* 2016.07.30, created by Eric.Wang
*/


/*
* get data
*/
var getUserListData = function(){
	var data = {

	};
	var option = {
		url: '/draft/manager/list/v1',
		type: 'GET',
		data: data,
	};
	createAjax(option, function(err, res){
		if(!err){
			userListTable('userListTable', res);
			userOperationTableFilter(res);
		}
	});
};

var getUserOperationData = function(data, fn){
	var data = {
		userid: data.userid,
	};
	var option = {
		url: '/draft/manager/operations/v1',
		type: 'GET',
		data: data,
	};
	createAjax(option, function(err, res){
		if(!err){
			fn(null, res);
		}else{
			fn(err);
		}
	});
};

/*
* data filter
*/
function userOperationTableFilter(data, mergeOption){
	var operationList = [];
	var mergeOption = mergeOption || [];
	var lastIndex = 0;
	var lastLength = 0;
	$.each(data.list, function(index, item){
		getUserOperationData(item.userid, function(err, res){
			if(!err){
				$.each(res.list, function(index, each){
					each.userid = item.userid;
					each.username = item.username;
					each.role = item.role;
					each.start_day = res.start_day;
					each.end_day = res.end_day;
				});
				mergeOption.push({
					index: lastIndex + lastLength,
					rowspan: res.list.length
				});
				lastIndex = lastIndex + lastLength;
				lastLength = res.list.length;
				operationList = operationList.concat(res.list);
				if(mergeOption.length == data.list.length){
					userOperationTable('userOperationTable', operationList);
					mergeUserOperationCells(mergeOption);
				}
			}
		});
	});
}

function mergeUserOperationCells(option){
	$.each(option, function(index, item){
		$('#userOperationTable').bootstrapTable('mergeCells', {
			index: item.index,
			field: 'userid',
			colspan: 1, 
			rowspan: item.rowspan,
		});
		$('#userOperationTable').bootstrapTable('mergeCells', {
			index: item.index,
			field: 'username',
			colspan: 1, 
			rowspan: item.rowspan,
		});
		$('#userOperationTable').bootstrapTable('mergeCells', {
			index: item.index,
			field: 'role',
			colspan: 1, 
			rowspan: item.rowspan,
		});
		$('#userOperationTable').bootstrapTable('mergeCells', {
			index: item.index,
			field: 'start_day',
			colspan: 1, 
			rowspan: item.rowspan,
		});
		$('#userOperationTable').bootstrapTable('mergeCells', {
			index: item.index,
			field: 'end_day',
			colspan: 1, 
			rowspan: item.rowspan,
		});
	});
};

/*
* draw charts
*/
var userListTable = function(element, data){
	$('#' + element).bootstrapTable({
		data: data.list,
		classes: 'table table-hover',
		pagination: true,
		pageSize: 20,
		pageNumber: 1,
		pageList: [20, 50],
		sidePagination: 'client',
		showRefresh: true,
		showColumns: true,
		buttonsAlign: 'right',
		toolbar: '.userListTable',
		showExport: true,
		exportDataType: 'all',
		exportTypes: ['csv', 'txt', 'excel'],
		locale: 'zh-CN',
		columns: [
		{
			field: 'Number',  
	    title: '序号',  
	    formatter: function (value, row, index) {  
	    	return index + 1;  
	    }
		},
		{
			title: '用户名',
			field: 'username',
		}, {
			title: '单位名称',
			field: 'company_name',
		}, {
			title: '角色',
			field: 'role',
		}, {
			title: '有效',
			field: 'valid',
			formatter: function(value){
				return value == true ? '是' : '否';
			},
		}, {
			title: '分配日期',
			field: 'start_date',
		}, {
			title: '失活日期',
			field: 'expire_date',
		}],

	});
}

function userOperationTable(element, data){
	$('#' + element).bootstrapTable({
		data: data,
		classes: 'table table-hover',
		pagination: true,
		pageSize: 20,
		pageNumber: 1,
		pageList: [20, 50],
		sidePagination: 'client',
		showRefresh: true,
		showColumns: true,
		buttonsAlign: 'right',
		toolbar: '.userOperationTable',
		showExport: true,
		exportDataType: 'all',
		exportTypes: ['csv', 'txt', 'excel'],
		locale: 'zh-CN',
		columns: [
		{
			field: 'Number',  
	    title: '序号',  
	    formatter: function (value, row, index) {  
	    	return index + 1;  
	    }
		},
		{
			title: '登录名',
			field: 'userid',
			valign: 'middle',
		}, {
			title: '用户姓名',
			field: 'username',
			valign: 'middle',
		}, {
			title: '角色',
			field: 'role',
			valign: 'middle',
		}, {
			title: '开始日期',
			field: 'start_day',
			valign: 'middle',
		}, {
			title: '结束日期',
			field: 'end_day',
			valign: 'middle',
		}, {
			title: '操作日期',
			field: 'operate_date',
			valign: 'middle',
		}, {
			title: '操作内容',
			field: 'operate_content',
			valign: 'middle',
		}, {
			title: '操作人',
			field: 'operator',
			valign: 'middle',
		}],

	});
};

$(function(){
	getUserListData();
});